home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / memsrch.lib < prev    next >
Text File  |  1995-01-29  |  4KB  |  125 lines

  1. // MemSrch.lib - Provide MemSearch routine to use assembly
  2. // ver.1         to greatly speed-up search for sub-text
  3. //               within a bigger buffer
  4.  
  5.  
  6. MemSearchForMem(pBuffer,pBufferLen,pSubBuffer,pSubBufferLen,pCaseSensitive)
  7. {
  8.    //   public _thing
  9.    //
  10.    //ToUpperToLower PROC NEAR
  11.    //Upper_ah:
  12.    //   cmp   ah, 'a'
  13.    //   jb    Lower_al
  14.    //   cmp   ah, 'z'
  15.    //   ja    Lower_al
  16.    //   sub   ah, 'a' - 'A'
  17.    //Lower_al:
  18.    //   cmp   al, 'A'
  19.    //   jb    UpLowDone
  20.    //   cmp   al, 'Z'
  21.    //   ja    UpLowDone
  22.    //   add   ah, 'a' - 'A'
  23.    //UpLowDone:
  24.    //   ret
  25.    //ToUpperToLower ENDP
  26.    //
  27.    //
  28.    //_thing PROC FAR
  29.    //
  30.    //   ; assume ax is segment of big buffer
  31.    //   ; assume bx is segment of little buffer
  32.    //   ; assume cx is length to search first character in big buffer
  33.    //   ; assume dx is length of sub-buffer
  34.    //
  35.    //   ; set di to location of big buffer so ax:di is big buffer
  36.    //   mov   di, 0
  37.    //   ; set si to location of sub-buffer so bx:si is sub-buffer
  38.    //   mov   si, 0
  39.    //
  40.    //   ; get ds:di to big buffer, and es:si to sub-buffer
  41.    //   mov   ds, ax
  42.    //   mov   es, bx
  43.    //
  44.    //   ; set al and ah to the byte may starts the sub-buffer, assume
  45.    //   ; case insensitive, but CEnvi may overwrite that
  46.    //   mov   al, es:[si]
  47.    //   mov   ah, al
  48.    //   call  ToUpperToLower
  49.    //
  50.    //IsThisTheFirstByte:
  51.    //   cmp   al, [di]
  52.    //   je    FirstByteFound
  53.    //   cmp   ah, [di]
  54.    //   jne   NotFoundThisByte
  55.    //
  56.    //FirstByteFound:
  57.    //   ; found first byte, and so check all the other bytes for a match
  58.    //   push  di
  59.    //   push  ax
  60.    //   xor   bx, bx
  61.    //KeepLookingInSubBuffer:
  62.    //   inc   bx
  63.    //   inc   di
  64.    //   cmp   bx, dx
  65.    //   je    FoundIt
  66.    //   mov   al, es:[si+bx]
  67.    //   mov   ah, al
  68.    //   call  ToUpperToLower
  69.    //   cmp   al, [di]
  70.    //   je    KeepLookingInSubBuffer
  71.    //   cmp   ah, [di]
  72.    //   je    KeepLookingInSubBuffer
  73.    //
  74.    //NotThisSubBuffer:
  75.    //   pop   ax
  76.    //   pop   di
  77.    //
  78.    //NotFoundThisByte:
  79.    //   inc   di
  80.    //   loop  IsThisTheFirstByte
  81.    //
  82.    //   ; if here then buffer was never found; return NULL
  83.    //   xor   ax, ax
  84.    //   xor   dx, dx
  85.    //   jmp   ByeBye
  86.    //
  87.    //FoundIt:
  88.    //   ; Yea! it was found; return address where it was found
  89.    //   pop   ax
  90.    //   pop   ax       ; ax = di becaue had pushed di
  91.    //   mov   dx, ds
  92.    //
  93.    //ByeBye:
  94.    //   ret
  95.    //
  96.    //_thing ENDP
  97.    lAsmCode =
  98.       "\x80\xFC\x61\x72\x08\x80\xFC\x7A\x77\x03\x80\xEC\x20\x3C\x41\x72"
  99.       "\x07\x3C\x5A\x77\x03\x80\xC4\x20\xC3\xBF\x00\x00\xBE\x00\x00\x8E"
  100.       "\xD8\x8E\xC3\x26\x8A\x04\x88\xC4\xE8\xD5\xFF\x3A\x05\x74\x04\x3A"
  101.       "\x25\x75\x1C\x57\x50\x31\xDB\x43\x47\x39\xD3\x74\x1B\x26\x8A\x00"
  102.       "\x88\xC4\xE8\xBB\xFF\x3A\x05\x74\xEE\x3A\x25\x74\xEA\x58\x5F\x47"
  103.       "\xE2\xD9\x31\xC0\x31\xD2\xEB\x04\x58\x58\x8C\xDA\xCB";
  104.    // only search first byte up to first one that can fit entire SubBuffer
  105.    lSearchLen = pBufferLen - pSubBufferLen + 1;
  106.    if ( lSearchLen < 0 )
  107.       return NULL;
  108.    // make local copy of assembly so we can change it
  109.    memcpy(lAsm,lAsmCode,1+GetArraySpan(lAsmCode));
  110.    // if case-sensitive then do have case-sensitive sub-routine do nothing
  111.    if ( pCaseSensitive )
  112.       lAsm[0] = '\xC3';
  113.    // set di in code to offst of Buffer, and si to offset of SubBuffer
  114.    BLObPut(lAsm,0x1A,offset(pBuffer),UWORD16);
  115.    BLObPut(lAsm,0x1D,offset(pSubBuffer),UWORD16);
  116.    // call the routine with ax, bx, cx, and dx as assebmly likes it
  117.    lFound = asm(lAsm+0x19,segment(pBuffer),segment(pSubBuffer),
  118.                 lSearchLen,pSubBufferLen);
  119.    if ( !lFound )
  120.       return NULL;
  121.    // return variable relative to pBuffer
  122.    return ( pBuffer + (lFound-pointer(pBuffer)) );
  123. }
  124.  
  125.